package com.ysd.lis.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ysd.common.Result;
import com.ysd.common.dto.ColumnAndCommentDto;
import com.ysd.lis.entity.WkCalcElement;
import com.ysd.lis.mapper.WkCalcElementMapper;
import com.ysd.lis.service.WkCalcElementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 考核要素 服务实现类
 * </p>
 *
 * @author LiuQi
 * @since 2024-04-19
 */
@Service
public class WkCalcElementServiceImpl extends ServiceImpl<WkCalcElementMapper, WkCalcElement> implements WkCalcElementService {

    @Autowired
    WkCalcElementMapper wkCalcElementMapper;

    @Override
    public Result getCalcElementList(String keyWord) {
        LambdaQueryWrapper<WkCalcElement> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WkCalcElement::getDelFlag, 0)
                .and(StrUtil.isNotEmpty(keyWord), wrapper -> wrapper.like(WkCalcElement::getElementCode, keyWord).or().like(WkCalcElement::getElementName, keyWord));
        List<WkCalcElement> wkCalcElements = wkCalcElementMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功！", wkCalcElements);
    }

    @Override
    public Result findTableNamesByDataBase(String dataBaseName) {
        List<Map<String, String>> tableNamesByDataBase = wkCalcElementMapper.findTableNamesByDataBase(dataBaseName);
        return Result.succ(1, "查询成功！", tableNamesByDataBase);
    }

    /**
     * 查询表字段
     * @param tableName
     * @return
     */
    @Override
    public Result findColumnsByTableName(String tableName) {
        List<ColumnAndCommentDto> columnsByTableName = wkCalcElementMapper.findColumnsByTableName(tableName);
        return Result.succ(1, "查询成功！", columnsByTableName);
    }

    @Override
    public Result addCalcElement(WkCalcElement wkCalcElement) {
        boolean save = this.save(wkCalcElement);
        if (save) {
            return Result.succ(1, "添加成功！", save);
        } else {
            return Result.fail("添加失败");
        }
    }

    @Override
    public Result editCalcElement(WkCalcElement wkCalcElement) {
        boolean save = this.updateById(wkCalcElement);
        if (save) {
            return Result.succ(1, "编辑成功！", save);
        } else {
            return Result.fail("编辑失败");
        }
    }

    @Override
    public Result deleteCalcElement(List<String> ids) {
        boolean save = this.removeByIds(ids);
        if (save) {
            return Result.succ(1, "删除成功！", save);
        } else {
            return Result.fail("删除失败");
        }
    }
}
